
typedef	std::list<struct DnsRecord *>	DnsRecordsByDate;
typedef	DnsRecordsByDate::iterator	DnsRecordsByDateIter;
typedef	DnsRecordsByDate::reverse_iterator	DnsRecordsByDateReverseIter;

struct DnsRecord {
	struct in_addr ip_m;
	std::string	hostName_m;
	int	serial_m;
	time_t last_access_m;
	DnsRecordsByDateIter	byDate_m;
public:
	DnsRecord(struct in_addr ip, std::string& hostName, int serial, time_t last_access);
	DnsRecord(struct in_addr ip, std::string hostName);
	friend std::ostream& operator<<(std::ostream &os, const DnsRecord *cr) {
		char	inet_char[inet_to_stringSize];
		inet_to_char(cr->ip_m, inet_char);
		os << "src " << inet_char
			<< " host " << cr->hostName_m
			<< " serial " << cr->serial_m
			<< " last " << ctime(&cr->last_access_m);
		return os;
	}
	friend class DnsCache;
};

struct CompareDnsRecord  {
    bool operator()(const DnsRecord *s1, const DnsRecord* s2) const {
		if (s1->ip_m.s_addr == s2->ip_m.s_addr) {
			return s1->hostName_m < s2->hostName_m;
		} else {
			return s1->ip_m.s_addr < s2->ip_m.s_addr;
		}
    }
};


typedef std::map<DnsRecord *, DnsRecord *, CompareDnsRecord> DnsRecordMap;
typedef DnsRecordMap::iterator DnsRecordMapIter;

typedef std::map<uint32_t,  DnsRecord *> DnsRecordsByIp;
typedef DnsRecordsByIp::iterator DnsRecordsByIpIterator;


class DnsCache {
	DnsRecordMap	ipToRecord_m;
	DnsRecordsByDate	recordsByDate_m;
	DnsRecordsByIp	recordsByIp_m;
	int	expiryTime_m;
	pthread_mutex_t mutex_;
public:
	DnsCache(int expiryTime) : expiryTime_m(expiryTime) {
		pthread_mutex_init(&mutex_, NULL);
	}
	// find the host from the IP 
	int	FindHostFromIP(const struct in_addr& ip, const char *mac, class SQLData *sdc);
	void FlushOld();
};
	

